<?php require("user_settings.php"); ?>
<?php
	$uname="";$passwd="";
	if(isset($_GET['usernamesignup']))
		$uname=htmlspecialchars($_GET['usernamesignup']);
	if(isset($_GET['passwordsignup']))
		$passwd=htmlspecialchars($_GET['passwordsignup']);

	if(strlen($passwd) == 0) {
		echo "Password is empty";
		exit ;
	}
	if(strlen($uname) == 0) {
		echo "User is empty";
		exit ;
	}

	#$users = json_decode(file_get_contents('passwd.json'),true);
	$ret = verifyUser($uname, $passwd);
	if($ret == "ERR_USER_NONEXIST") {
		if(addUser($uname, $passwd) != "OK_OK") 
			echo "Add failed<BR>";
		#$users[$uname] = $passwd;
		#file_put_contents("passwd.json",json_encode($users));
		echo "$uname is registerd successfully.<BR>";
		#mkdir("users/".$uname);
		#touch("users/".$uname."/settings.json");
		#echo "<script lang=javascript> document.location.href=(\"topframe.php?local=$uname\");</script>";
		session_save_path("./.php_session");
		session_start();
		$_SESSION["login"] = true;
		$_SESSION["id"] = "$uname";
		echo "<script lang=javascript> document.location.href=(\"init_settings.php\");</script>";
		
	}
	else if(($ret == "ERR_USER_PASSWD_MISMATCH") || ($ret == "OK_OK")) {
		echo "<script lang=javascript> document.location.href=(\"comp_admin.php?err='使用者 $uname 已經被註冊了'#toregister\");</script>";
	}

	
?>
